package com.example.app08_listview;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MainActivity
 * @author lijiguo
 */
public class MainActivity_SimpleAdapter extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");

        ListView lvMain = findViewById(R.id.lv_main);
        // 准备集合数据
        List<Map<String, Object>> data = new ArrayList<>(20);
        // 填充数据
        for (int i = 1; i <= 20; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("icon", R.drawable.food);
            map.put("name", "food-name-" + i);
            map.put("content", "food-content-" + i);
            data.add(map);
        }

        String[] from = {"icon", "name", "content"};
        int[] to = {R.id.iv_simple_item_icon, R.id.tv_simple_item_name, R.id.tv_simple_item_content};

        // 准备SimpleAdapter对象
        // context 上下文对象
        // data 数据集合,数据类型为 List<Map<String, Object>>
        // resource 布局资源文件
        // from map数据的key，string[]
        // to 布局内要显示控件的ID，int[]
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_simple_adapter,
                from, to);

        // 设置adapter
        lvMain.setAdapter(adapter);
    }
}